package com.spring.security.controller;

import com.spring.security.model.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.TimeZone;

/**
 * @author HouKunLin
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 服务器本地时区信息
     *
     * @return JsonResult JSON数据
     */
    @GetMapping("timeZone")
    public JsonResult timeZone() {
        TimeZone timeZone = TimeZone.getDefault();
        JsonResult json = JsonResult.ok();
        json.data()
                .put("ID", timeZone.getID())
                .put("displayName", timeZone.getDisplayName())
                .put("zoneId", timeZone.toZoneId())
                .put("DSTSavings", timeZone.getDSTSavings())
                .put("useDaylightTime", timeZone.useDaylightTime())
                .put("observesDaylightTime", timeZone.observesDaylightTime());
        return json;
    }

    @GetMapping("password/encode")
    public JsonResult passwordEncoderEncode(String password) {
        return JsonResult.ok().data()
                .put("password", passwordEncoder.encode(password)).result();
    }

    @GetMapping("password/matches")
    public JsonResult passwordEncoderEncode(String rawPassword, String encodedPassword) {
        return JsonResult.ok().data()
                .put("matches", passwordEncoder.matches(rawPassword, encodedPassword)).result();
    }
}
